package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.DishesRepository;
import com.qf.pojo.req.ShopCart;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.Dishes;
import com.qf.service.ShopCartService;
import com.qf.utils.CookieUtils;
import com.qf.utils.JwtUtils;
import com.qf.utils.RedisUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    DishesRepository dishesRepository;

    static String shopCart="SHOP_CART_";

    @Override
    public BaseResp addShopCart(Integer dishesId, HttpServletRequest request) {
        BaseResp baseResp = new BaseResp();
        //使用request得到cookie中的token值，使用jwt解析获取到用户的id
        CookieUtils cookieUtils = new CookieUtils();
        String uuid = cookieUtils.getToken(request);
        String token = (String)redisTemplate.opsForValue().get(uuid);
        JwtUtils jwtUtils = new JwtUtils();
        Map map = jwtUtils.Verifier(token);
        String userId = (String)map.get("id");
        //判断一下用户点的菜品是否未下架
        Optional<Dishes> byId = dishesRepository.findById(dishesId);
        if(!byId.isPresent()){
            baseResp.setCode(201);
            baseResp.setMessage("菜品已下架");
        }
        //往redis中存购物车，使用hash存储结构
        Object o = redisTemplate.opsForHash().get(shopCart + userId, dishesId.toString());
        if(o!=null){
            //已存在，改变数量
            String string = JSONObject.toJSONString(o);
            ShopCart shopCart1 = JSONObject.parseObject(string, ShopCart.class);
            shopCart1.setNum(shopCart1.getNum()+1);
            redisTemplate.opsForHash().put(shopCart + userId, dishesId.toString(),shopCart1);
            baseResp.setCode(200);
            baseResp.setMessage("添加成功");
            return baseResp;
        }else {
            //购物车中无商品时
            Dishes dishes = byId.get();
            ShopCart shopCart2 = new ShopCart();
            BeanUtils.copyProperties(dishes,shopCart2);
            shopCart2.setNum(1);
            redisTemplate.opsForHash().put(shopCart + userId, dishesId.toString(),shopCart2);
            baseResp.setCode(200);
            baseResp.setMessage("添加成功");
            return baseResp;
        }
    }

    @Override
    public BaseResp removeShopCart(HttpServletRequest request) {
        BaseResp baseResp = new BaseResp();
        //使用request得到cookie中的token值，使用jwt解析获取到用户的id
        CookieUtils cookieUtils = new CookieUtils();
        String uuid = cookieUtils.getToken(request);
        String token = (String)redisTemplate.opsForValue().get(uuid);
        JwtUtils jwtUtils = new JwtUtils();
        Map map = jwtUtils.Verifier(token);
        String userId = (String)map.get("id");
//        Object o = redisTemplate.opsForHash().get(shopCart + userId, dishesId.toString());
//        String string = JSONObject.toJSONString(o);
//        ShopCart shopCart1 = JSONObject.parseObject(string, ShopCart.class);
        redisTemplate.delete(shopCart + userId);
        baseResp.setCode(200);
        baseResp.setMessage("移除成功");
        return baseResp;
    }

    @Override
    public BaseResp delShopCart(Integer dishesId, HttpServletRequest request) {
        BaseResp baseResp = new BaseResp();
        //使用request得到cookie中的token值，使用jwt解析获取到用户的id
        CookieUtils cookieUtils = new CookieUtils();
        String uuid = cookieUtils.getToken(request);
        String token = (String)redisTemplate.opsForValue().get(uuid);
        JwtUtils jwtUtils = new JwtUtils();
        Map map = jwtUtils.Verifier(token);
        String userId = (String)map.get("id");
        Object o = redisTemplate.opsForHash().get(shopCart + userId, dishesId.toString());
        String string = JSONObject.toJSONString(o);
        ShopCart shopCart1 = JSONObject.parseObject(string, ShopCart.class);
        if(shopCart1.getNum()>1){
            shopCart1.setNum(shopCart1.getNum()-1);
            redisTemplate.opsForHash().put(shopCart + userId, dishesId.toString(),shopCart1);
        }else {
            redisTemplate.opsForHash().delete(shopCart + userId, dishesId.toString());
        }
        baseResp.setCode(200);
        baseResp.setMessage("删除成功");
        return baseResp;
    }

    @Override
    public BaseResp findShopCart(HttpServletRequest request) {
        BaseResp baseResp = new BaseResp();
        //使用request得到cookie中的token值，使用jwt解析获取到用户的id
        CookieUtils cookieUtils = new CookieUtils();
        String uuid = cookieUtils.getToken(request);
        String token = (String)redisTemplate.opsForValue().get(uuid);
        JwtUtils jwtUtils = new JwtUtils();
        Map map = jwtUtils.Verifier(token);
        String userId = (String)map.get("id");
        List list = redisTemplate.opsForHash().values(shopCart + userId);
        int i = 0;
        double price = 0.00;
        for (Object object:list
             ) {
            String string = JSONObject.toJSONString(object);
            ShopCart shopCart1 = JSONObject.parseObject(string, ShopCart.class);
            i+=shopCart1.getNum();
            price+=shopCart1.getPrice()*shopCart1.getNum();
        }
        baseResp.setCode(200);
        baseResp.setMessage("查询成功");
        baseResp.setData(list);
        baseResp.setTotal((long)i);
        baseResp.setPrice(price);
        return baseResp;
    }
}
